<template>
  <div class="login">
    <cp-nav-bar middle="登录" right="注册" @click-right="clickRight"></cp-nav-bar>
    <div class="login">
      <div class="login-head">
        <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
        <a href="javascript:;" @click="isPass = !isPass">
          <span>{{ !isPass ? '密码登录' : '短信验证码登录' }}</span>
          <van-icon name="arrow"></van-icon>
        </a>
      </div>
      <!-- == form 表单 == -->
      <van-form autocomplete="off" @submit="login" ref="form">
        <van-field
          :rules="mobileRules"
          v-model="mobile"
          placeholder="请输入手机号"
          type="tel"
          name="mobile"
        ></van-field>
        <!-- 密码 -->
        <van-field
          v-if="isPass"
          v-model="password"
          :rules="passwordRules"
          placeholder="请输入密码"
          :type="isShowPass ? 'text' : 'password'"
        >
          <!-- 眼睛图标 -->
          <template #button>
            <cp-icon
              @click="isShowPass = !isShowPass"
              :name="`login-eye-${isShowPass ? 'on' : 'off'}`"
            ></cp-icon>
          </template>
        </van-field>
        <!-- 验证码 -->
        <van-field v-model="code" :rules="codeRules" v-else placeholder="短信验证码">
          <template #button>
            <span class="btn-send" @click="ClickCode">{{
              time > 0 ? `${time}s后可再次发送` : '发送验证码'
            }}</span>
          </template>
        </van-field>
        <div class="cp-cell">
          <van-checkbox v-model="agree">
            <span>我已同意</span>
            <a href="javascript:;">用户协议</a>
            <span>及</span>
            <a href="javascript:;">隐私条款</a>
          </van-checkbox>
        </div>
        <div class="cp-cell">
          <!-- 设置为原生提交按钮 -->
          <van-button block round type="primary" native-type="submit">登 录</van-button>
        </div>
        <div class="cp-cell">
          <a href="javascript:;">忘记密码？</a>
        </div>
      </van-form>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, onUnmounted } from 'vue'
// 表单规则
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
import { Toast, type FormInstance } from 'vant'
// 用户接口
import { loginPsAPI, codeLoginAPI, codeFineLoginAPI } from '@/api/user'
// store
import { useUserStore } from '@/stores'
// router
import { useRouter, useRoute } from 'vue-router'

const store = useUserStore()
const router = useRouter()
const route = useRoute()
const clickRight = () => {
  console.log('点击注册')
}
const agree = ref(false)
// 控制眼睛
const isShowPass = ref(false)
const mobile = ref('13230000001')
const password = ref('abc12345')
const code = ref('')
// 点击登陆
const login = async () => {
  if (!agree.value) return Toast.fail('用户协议未勾选')
  try {
    const { data: res } = isPass.value
      ? await loginPsAPI(mobile.value, password.value)
      : await codeFineLoginAPI(mobile.value, code.value)
    console.log('登陆获取数据', res)
    store.setUserData(res)
    router.replace((route.query.returnUrl as string) || '/user')
    Toast.success('登陆成功')
  } catch (error) {
    console.log(error)
  }
}
// 页面切换短信验证码登录
const isPass = ref(true)
const time = ref(0)
const form = ref<FormInstance>()
let timeId: number
// 点击发送验证码
const ClickCode = async () => {
  if (time.value > 0) return
  // 对电话进行验证
  await form.value?.validate('mobile')
  // 请求验证码
  const res = await codeLoginAPI(mobile.value, 'login')
  console.log('验证码', res)
  Toast.success('验证码发送成功')
  // 倒计时
  time.value = 60
  timeId = window.setInterval(() => {
    time.value--
    if (time.value <= 0) {
      return window.clearInterval(timeId)
    }
    console.log(time.value)
  }, 1000)
}
// 防止内存泄漏
onUnmounted(() => {
  window.clearInterval(timeId)
})
</script>
<style lang="scss" scoped>
// 自动全局注册组件
.login {
  padding-top: 46px;
  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  .van-form {
    padding: 0 14px;
    .cp-cell {
      height: 52px;
      padding: 14px 16px;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      .van-checkbox {
        a {
          color: var(--cp-primary);
          padding: 0 5px;
        }
      }
    }
  }
}
</style>
